import { z } from "zod";
import path from "path";
import { fileURLToPath } from "url";
import { getDeepAnalyzeProblemPrompt } from "../../prompts/index.js";
import { getMemoryDir } from "../../utils/paths.js";

// 深入分析问题工具
// Deep Analyze Problem Tool
export const deepAnalyzeProblemSchema = z.object({
  problemSummary: z
    .string()
    .min(10, {
      message: "问题摘要不能少于10个字符，请提供更详细的问题描述",
    })
    .describe("问题的结构化摘要，来自详细分析阶段的结果"),
  analysisMethod: z
    .array(z.enum(['5why', 'fishbone', 'fault_tree', 'root_cause', 'swot']))
    .optional()
    .default(['5why', 'root_cause'])
    .describe("分析方法选择列表"),
  analysisDimensions: z
    .array(z.enum(['technical', 'business', 'resource', 'risk', 'timeline', 'stakeholder']))
    .optional()
    .default(['technical', 'business', 'risk'])
    .describe("分析维度选择列表"),
  complexityLevel: z
    .enum(['simple', 'moderate', 'complex', 'very_complex'])
    .optional()
    .default('moderate')
    .describe("问题复杂度级别")
});

export async function deepAnalyzeProblem({
  problemSummary,
  analysisMethod = ['5why', 'root_cause'],
  analysisDimensions = ['technical', 'business', 'risk'],
  complexityLevel = 'moderate'
}: z.infer<typeof deepAnalyzeProblemSchema>) {
  // 获取基础目录路径
  // Get base directory path
  const __filename = fileURLToPath(import.meta.url);
  const __dirname = path.dirname(__filename);
  const PROJECT_ROOT = path.resolve(__dirname, "../../..");
  const MEMORY_DIR = await getMemoryDir();

  // 使用prompt生成器获取最终prompt
  // Use prompt generator to get the final prompt
  const prompt = await getDeepAnalyzeProblemPrompt({
    problemSummary,
    analysisMethod,
    analysisDimensions,
    complexityLevel,
    memoryDir: MEMORY_DIR,
  });

  return {
    content: [
      {
        type: "text" as const,
        text: prompt,
      },
    ],
  };
}